package com.sharedlease.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.sharedlease.Feign.MerchantClient;
import com.sharedlease.Feign.ProductClient;
import com.sharedlease.entity.*;
import com.sharedlease.service.AdministratorService;
import com.sharedlease.service.ChatService;
import com.sharedlease.service.ProductService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Base64Utils;
import org.springframework.web.bind.annotation.*;

import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 吴钟富
 * @since 2021-03-02
 */
@RestController
@Api(tags = "管理员相关接口")
@RequestMapping("/administrator")
public class AdministratorController {
    @Autowired
    public AdministratorService administratorService;
    @Autowired
    public MerchantClient merchantClient;
    @Autowired
    public ProductClient productClient;

    @Autowired
    public ProductService productService;

    @Autowired
    public ChatService chatService;


    @GetMapping("/getById/{id}")
    @ApiOperation("根据id查询管理员")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "要查询的管理员id", defaultValue = "1"),

    }
    )
    public Administrator getById(@PathVariable Long id) {
        Administrator administrator = administratorService.getById(id);
        return administrator;
    }

    @GetMapping("/list")
    @ApiOperation("查询全部管理员")
    public List<Administrator> list() {
        List<Administrator> list = administratorService.list(null);
        return list;
    }

    @PostMapping("/save")
    @ApiOperation("新增一个管理员")
    public R save(@RequestBody Administrator administrator) {
        administrator.password= Base64Utils.encodeToUrlSafeString(administrator.password.getBytes());
        return R.operate(administratorService.save(administrator));
    }

    @PutMapping("/update")
    @ApiOperation("根据id更改管理员")
    public R update(@RequestBody Administrator administrator) {
        if (administratorService.updateById(administrator)) {
            return R.ok();
        }
        return R.error();
    }


    @DeleteMapping("/removeById/{id}")
    @ApiOperation("根据id删除一个管理员")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "要删除的管理员id", defaultValue = "-1"),

    }
    )
    public R removeById(@PathVariable Long id) {

        return R.operate(administratorService.removeById(id));
    }

    @GetMapping("/getMerchantById/{id}")
    @ApiOperation("根据id得到一个厂商")
    public Merchant getMerchantById(@PathVariable Long id) {
        return merchantClient.getMerchantById(id);

    }

    @GetMapping("/getMerchantList")
    @ApiOperation("得到所有厂商")
    public List<Merchant> getMerchantList() {
        return merchantClient.getMerchantList();

    }

    @PostMapping("/findByNameAndPwd/{name}/{password}")
    @ApiOperation("根据用户名和密码得到管理员")
    public Administrator findByNameAndPwd(@PathVariable String name,@PathVariable String password){
        password= Base64Utils.encodeToUrlSafeString(password.getBytes());
        QueryWrapper<Administrator> wrapper = new QueryWrapper<>();
        wrapper.eq("name",name);
        wrapper.eq("password",password);
        return administratorService.getOne(wrapper);

    }

    @GetMapping("/checkExistByName/{name}")
    @ApiOperation("判断管理员用户名是否存在")
    public boolean checkExistByName(@PathVariable String name){
        QueryWrapper<Administrator> wrapper = new QueryWrapper<>();
        wrapper.eq("name",name);
        Administrator one;
        one= administratorService.getOne(wrapper);
        if (one!=null){
            return true;
        }
        else{
            return false;
        }

    }


    @PostMapping("/saveMerchant")
    @ApiOperation("新增一个厂商")
    public R saveMerchant(@RequestBody Merchant merchant) {
        return merchantClient.saveMerchant(merchant);

    }

    @PostMapping("/deleteMerchantById")
    @ApiOperation("根据id删除一个厂商")
    public R deleteMerchant(@RequestBody Merchant merchant) {
        return merchantClient.deleteMerchantById(merchant);

    }

    @PutMapping("/updateMerchant")
    @ApiOperation("根据id更改一个厂商")
    public R updateMerchant(@RequestBody Merchant merchant) {
        return merchantClient.updateMerchantById(merchant);
    }

    @PostMapping("/getProductById")
    @ApiOperation("根据id查询产品")
    public Product getProductById(@PathVariable Long id) {
        return productClient.getProductById(id);

    }

    @GetMapping("/getProductList")
    @ApiOperation("得到所有产品")
    public List<Product> getProductList() {
        return productClient.getProductList();

    }

    @PostMapping("/deleteProductById")
    @ApiOperation("根据id删除一个产品")
    public R deleteProduct(@RequestBody Product product) {
        return productClient.deleteProductById(product);

    }

    @PostMapping("/success")
    @ApiOperation("审核通过")
    public R reviewSuccess(@RequestBody Admin_Product admin_product){
        Product product = admin_product.getProduct();
        Administrator admin = admin_product.getAdministrator();
        product.setLegal(1);
        productService.updateById(product);
        // 添加信息到聊天表
        String s = "审核通过";
        Chat chat = new Chat(IdWorker.getId(), admin.getId(), admin.getName(), product.getMid(), s, 0);
        chatService.save(chat);
        return R.ok();
    }

    @PostMapping("/fail")
    @ApiOperation("审核未通过")
    public R reviewFail(@RequestBody Admin_Product admin_product){
        productService.removeById(admin_product.getProduct().getId());
        // 添加信息到聊天表
        String s = "审核未通过";
        Chat chat = new Chat(IdWorker.getId(), admin_product.getAdministrator().getId(), admin_product.getAdministrator().getName(), admin_product.getProduct().getMid(), s, 0);
        chatService.save(chat);
        return R.ok();
    }


}

